package com.yxx.data.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yxx.data.domain.Complication;
import com.yxx.data.service.ComplicationService;
import com.yxx.data.vo.ComplicationVo;
import com.yxx.sys.common.AppSecureUtils;
import com.yxx.sys.common.DataGridView;
import com.yxx.sys.common.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDate;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yxx
 * @since 2022-07-03
 */
@RestController
@RequestMapping("/complication")
@Api("并发症控制器")
public class ComplicationController {

  @Autowired
  ComplicationService complicationService;

  @GetMapping("/loadAllInformation")
  @ApiOperation("查询并发症")
  public DataGridView loadAllInformation(ComplicationVo complicationVo) {
    IPage<Complication> page = new Page<Complication>(complicationVo.getPage(), complicationVo.getLimit());
    QueryWrapper<Complication> queryWrapper = new QueryWrapper<Complication>();
    queryWrapper.eq(StrUtil.isNotBlank(complicationVo.getHospitalId()), "hospital_id", complicationVo.getHospitalId());
    queryWrapper.orderByDesc("id");
    complicationService.page(page, queryWrapper);
    return new DataGridView(page.getTotal(), page.getRecords());
  }

  @PostMapping("/addInformation")
  @ApiOperation("添加并发症")
  public Result addInformation(ComplicationVo complicationVo) {
    complicationVo.setName(AppSecureUtils.getEncryptData(complicationVo.getName()));

    complicationVo.setCreatedTime(LocalDate.now());
    complicationVo.setChangeTime(LocalDate.now());
    boolean succ = complicationService.save(complicationVo);
    if(!succ) return Result.ADD_REEOR;
    return Result.ADD_SUCCESS;
  }

  @PostMapping("/updateInformation")
  @ApiOperation("更新并发症")
  public Result updateInformation(ComplicationVo complicationVo) {
    try {
      boolean b = complicationService.updateById(complicationVo);
      if(!b) return Result.UPDATE_REEOR;
      return Result.UPDATE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.UPDATE_REEOR;
    }
  }

  @PostMapping("/deleteInformation")
  @ApiOperation("删除并发症")
  public Result deleteInformation(Integer id) {
    try {
      complicationService.removeById(id);
      return Result.DELETE_SUCCESS;
    } catch (Exception e) {
      e.printStackTrace();
      return Result.DELETE_REEOR;
    }
  }

}
